package cn.com.open.itembank.mapper;

import cn.com.open.itembank.dto.RulesSearchModel;
import cn.com.open.itembank.entity.RuleContent;
import cn.com.open.itembank.entity.Rules;
import cn.com.open.itembank.sqlprovider.RulesSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * The interface Rules mapper.
 */
@Mapper
public interface RulesMapper {

    /**
     * 根据条件获取策略列表总条数.
     *
     * @param rulesModel the rules model
     * @return the list count
     */
    @SelectProvider(type = RulesSqlProvider.class, method = "getRulesCount")
    int getListCount(@Param("RulesModel") RulesSearchModel rulesModel);

    /**
     * 根据条件获取策略列表分页信息.
     *
     * @param rulesModel the rules model
     * @return the rules
     */
    @SelectProvider(type = RulesSqlProvider.class, method = "getRulesQuery")
    List<Rules> getRules(@Param("RulesModel") RulesSearchModel rulesModel);

    /**
     * 根据策略ID获取策略.
     *
     * @param id the id
     * @return the rules by id
     */
    @Select("SELECT * FROM Rules WHERE RuleID = #{id} and Status <> -1")
    Rules getRulesById(@Param("id") String id);

    /**
     * 解除策略与试题的关系.
     *
     * @param id the id
     */
    @Delete("DELETE FROM QuestionRule WHERE RuleID = #{id}")
    void deleteQuestionRulesRelation(@Param("id") String id);

    /**
     * 删除策略.
     *
     * @param id         the id
     * @param itemBankId the item bank id
     * @return the int
     */
    @Update("UPDATE Rules SET Status = -1 WHERE RuleID = #{id} and CourseID = #{itemBankId}")
    int deleteRules(@Param("id") String id, @Param("itemBankId") String itemBankId);

    /**
     * 修改策略状态.
     *
     * @param ruleID the rule id
     * @param status the is active
     * @return the int
     */
    @Update("UPDATE Rules SET Status = ${status} WHERE RuleID = #{ruleID}")
    int updateStatus(@Param("ruleID") String ruleID, @Param("status") int status);


    /**
     * 添加策略.
     *
     * @param rules the rules
     * @return the int
     */
    @Insert("INSERT INTO Rules (RuleID,RuleName,RuleType,courseID,OrganizationID,Score,ScoreLimit,DisplayCount,RuleVersion,Creater,CreateTime,Status,IsCheck,TimeLimited,Scale) VALUES (#{ruleID},#{ruleName},#{ruleType},#{courseID},#{organizationID},#{score},#{scoreLimit},#{displayCount},#{ruleVersion},#{creater},#{createTime},#{status},#{isCheck},#{timeLimited},#{scale})")
    int addRules(Rules rules);

    /**
     * 修改策略.
     *
     * @param rules the rules
     * @return the int
     */
    @Update("UPDATE Rules SET RuleName=#{ruleName},OrganizationID=#{organizationID},Score=#{score},TimeLimited=#{timeLimited },Modifier=#{modifier},ModifyTime=#{modifyTime},LastUpdateTime=#{lastUpdateTime},RuleVersion=#{ruleVersion},Scale=#{scale} WHERE RuleID=#{ruleID} ")
    int updateRules(Rules rules);

    /**
     * 添加装载策略内容.
     *
     * @param ruleID     the ruleID
     * @param contentXml the contentXml
     * @return the int
     */
    @Insert("INSERT INTO RuleContent (ruleID,ContentXml) VALUES (#{ruleID},#{contentXml})")
    int addRuleContent(@Param("ruleID") String ruleID, @Param("contentXml") String contentXml);

    /**
     * 修改装载策略内容.
     *
     * @param ruleID     the ruleID
     * @param contentXml the contentXml
     * @return the int
     */
    @Update("UPDATE RuleContent SET contentXml = #{contentXml} WHERE ruleID = #{ruleID}")
    int updateRuleContent(@Param("ruleID") String ruleID, @Param("contentXml") String contentXml);


    /**
     * 获取RuleContent信息
     * @param ruleID    the ruleID
     * @return          the RuleContent
     */
    @Select("SELECT * FROM RuleContent WHERE ruleID = #{ruleID}")
    RuleContent getRuleContentById(@Param("ruleID") String ruleID);

}
